package com.some.jobdescapp.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;

import com.some.jobdescapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author RANDY.ZHViewPagerANG
 * @version 1.0
 * @company:
 * @date 2018/12/10 18:11
 */
public class PhotoPager extends FrameLayout implements PhotoAdapter.OnKliaoProfilePhotoPagerClickListener, ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private PointHintView pointHintView;
    private PhotoAdapter adapter;
    private List<String> paths = new ArrayList<>();
    private List<Integer> ids = new ArrayList<>();
    public PhotoPager(Context context) {
        this(context, null);
    }

    public PhotoPager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PhotoPager(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.layout_photos, this);

    }

    public void init() {
        viewPager = findViewById(R.id.view_pager);
        pointHintView = findViewById(R.id.view_pager_indicator);
        adapter = new PhotoAdapter();
        adapter.setOnKliaoProfilePhotoPagerClickListener(this);
        viewPager.setAdapter(adapter);
        pointHintView.initView(adapter.getRealCount(), Gravity.CENTER);
        viewPager.addOnPageChangeListener(this);
    }

    public void refreshView(List<String> paths) {
        refreshView(paths, false);
    }

    public void refreshView(List<String> paths, boolean supportLoop) {
        if (paths == null) {
            paths = new ArrayList<>();
        }
        this.paths.clear();
        this.paths.addAll(paths);
        adapter.reFreshAll(paths, supportLoop);
        pointHintView.initView(adapter.getRealCount(), Gravity.CENTER);
        if (supportLoop) {
            viewPager.setCurrentItem(100 * adapter.getRealCount());
        } else {
            viewPager.setCurrentItem(0);
        }
    }

    public void refreshViewByIds(List<Integer> ids, boolean supportLoop) {
        if (paths == null) {
            paths = new ArrayList<>();
        }
        this.ids.clear();
        this.ids.addAll(ids);
        adapter.reFreshAllByIds(ids, supportLoop);
        pointHintView.initView(adapter.getRealCount(), Gravity.CENTER);
        if (supportLoop) {
            viewPager.setCurrentItem(100 * adapter.getRealCount());
        } else {
            viewPager.setCurrentItem(0);
        }
    }

    @Override
    public void onPhotoClick(int position) {
        if (paths.size() < position) {
            return;
        }
        if (onKliaoProfilePhotoPagerClickListener != null) {
            onKliaoProfilePhotoPagerClickListener.onKliaoProfilePhotoPagerClick(position, paths.get(position));
        }
    }

    private OnKliaoProfilePhotoPagerClickListener onKliaoProfilePhotoPagerClickListener;

    public void setOnKliaoProfilePhotoPagerClickListener(OnKliaoProfilePhotoPagerClickListener onKliaoProfilePhotoPagerClickListener) {
        this.onKliaoProfilePhotoPagerClickListener = onKliaoProfilePhotoPagerClickListener;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        pointHintView.setCurrent(position % adapter.getRealCount());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public interface OnKliaoProfilePhotoPagerClickListener{
        void onKliaoProfilePhotoPagerClick(int position, Object path);
    }
}
